//
//  LRComposeV.swift
//  WeiboX
//
//  Created by admin on 09/12/2016.
//  Copyright © 2016 iOS_Objective-C. All rights reserved.
//

import UIKit
import pop

class LRComposeV: UIView {
	
	private lazy var modelArr: [LRComposeMenu] = self.loadComposeMenuData()
	
	private lazy var bgImgV: UIImageView = UIImageView()
	
	private lazy var btnArr: [LRComposeBtn] = [LRComposeBtn]()
	
	private var target: UIViewController?

	override init(frame: CGRect) {
		super.init(frame: frame)
		
		setupUI()
	}
	
	required init?(coder aDecoder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}
	
	private func setupUI() {
		backgroundColor = .red
		self.size = CGSize(width: ScreenWidth, height: ScreenHeight)
		
		addSubview(bgImgV)
		bgImgV.snp.makeConstraints { (make) in
			make.leading.top.size.equalTo(self)
		}
		
		bgImgV.image = screenshot().applyLightEffect()
		addChildBtn()
	
	}
	
	private func loadComposeMenuData() -> [LRComposeMenu] {
		// 文件路径
		let path = Bundle.main.path(forResource: "compose.plist", ofType: nil)
		// 加载 plist
		let dicArr = NSArray(contentsOfFile: path!)! as! [[String: Any]]
		// 转模型数组
		let modelArr = NSArray.yy_modelArray(with: LRComposeMenu.self, json: dicArr) as! [LRComposeMenu]
		
		return modelArr
	}
	
	private func addChildBtn() {
		// 宽高
		let btnWith: CGFloat = 80
		let btnHeight: CGFloat = 100
		
		let btnMargin = (ScreenWidth - 3 * btnWith) / 4
		
		for i in 0..<6 {
			let btn = LRComposeBtn()
			
			let colIndex = i % 3
			let rowIndex = i / 3
			
			btn.tag = i
			btn.addTarget(self, action: #selector(btnAction(btn:)), for: .touchUpInside)
			
			// 座标
			btn.x = CGFloat(colIndex) * btnWith + CGFloat(colIndex + 1) * btnMargin
			btn.y = CGFloat(rowIndex) * btnHeight + CGFloat(rowIndex + 1) * btnMargin + ScreenHeight
			
			let model = modelArr[i]
			btn.setImage(UIImage(named: model.icon!), for: .normal)
			btn.setTitle(model.title!, for: .normal)
			btn.size = CGSize(width: btnWith, height: btnHeight)
			
			addSubview(btn)
			
			btnArr.append(btn)
		}
	}
	
	@objc private func btnAction(btn: LRComposeBtn) {
		UIView.animate(withDuration: 0.25, animations: {
			for button in self.btnArr {
				button.alpha = 0.2
				if button == btn {
					// 放大
					button.transform = CGAffineTransform(scaleX: 2, y: 2)
				} else {
					button.transform = CGAffineTransform(scaleX: 0.2, y: 0.2)
				}
			}
		}) { (_) in
			// 重置按钮
			UIView.animate(withDuration: 0.25, animations: {
				for button in self.btnArr {
					button.alpha = 1
					button.transform = CGAffineTransform.identity
				}
			}, completion: { (_) in
				let model = self.modelArr[btn.tag]
				let classType = NSClassFromString(model.className!) as! UIViewController.Type
				let vc = classType.init()
				let nav = LRNavController(rootViewController: vc)
				
				self.target?.present(nav, animated: true, completion: {
					self.removeFromSuperview()
				})
			})
		}
	}
	
	override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
		
		btnAnimation(isUp: false)
		
		DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
			self.removeFromSuperview()
		}
	}
	
	// 截屏的方法
	private func screenshot() -> UIImage {
		
		let window = UIApplication.shared.keyWindow!
		// 开启图片上下文
		UIGraphicsBeginImageContext(window.size)
		
		// 截屏操作
		window.drawHierarchy(in: CGRect.init(origin: CGPoint.zero, size: window.size) , afterScreenUpdates: false)
		
		// 从图片上下文中取出图片
		let screenshotImg = UIGraphicsGetImageFromCurrentImageContext()
//		let date = UIImageJPEGRepresentation(screenshotImg!, 0.5)!
//		try! date.write(to: URL.init(fileURLWithPath: "/Users/admin/Desktop/a.jpg"))

		// 结束图片上下文
		UIGraphicsEndImageContext()
		
		
		return screenshotImg!  
	}
	
	// 弹出动画
	private func btnAnimation(isUp: Bool) {
		if !isUp {
			btnArr = btnArr.reversed()
		}
		
		for (i, btn) in btnArr.enumerated() {
			let btnAnim = POPSpringAnimation(propertyNamed: kPOPViewCenter)
			if isUp {
				btnAnim?.toValue = CGPoint(x: btn.centerX, y: btn.centerY - 350)
			} else {
				btnAnim?.toValue = CGPoint(x: btn.centerX, y: btn.centerY + 350)
			}
			
			// 振幅
			btnAnim?.springBounciness = 10
			// v0
			btnAnim?.springSpeed = 10
			// 开始时间
			btnAnim?.beginTime = CACurrentMediaTime() + Double(i) * 0.025
			// 开始动画
			btn.pop_add(btnAnim, forKey: nil)
		}
	}
	
	// show 模糊界面的方法
	func show(target: UIViewController) {
		self.target = target
		
		target.view.addSubview(self)
		
		btnAnimation(isUp: true)
	}
}
